﻿// ----------------------------------------------------------------------
// <author name="Moses Kehengu"/>
// <copyright file="CustomizeChart.cs" company="Futures Group International">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Telerik.Charting;
using Telerik.Charting.Styles.Skins;

using GenericTools;

namespace ReportingManagement
{
    public partial class CustomizeChart : Form
    {
        private ChartSeriesCollection m_Series;
        private string m_Skin = "Default";
        private Color m_Color = Color.White;
        private Telerik.Charting.Styles.FillType m_FillType = Telerik.Charting.Styles.FillType.Solid;

        public CustomizeChart(ChartSeriesCollection seriesCollection)
        {
            InitializeComponent();
            m_Series = seriesCollection;

            var enums = Enum.GetValues(typeof(ChartSeriesType));
            foreach (var value in enums)
            {
                ListItem i = new ListItem();
                i.Value = value;
                i.Text = value.ToString();
                //TODO: CandleStick causes error. Refresh does not work on it.
                if (i.Text != "CandleStick") cboSeriesType.Items.Add(i);
            }

            ChartSkinsCollection skins = new ChartSkinsCollection();
            List<string> skinsList = skins.GetNames();
            foreach (string skin in skinsList)
            {
                ListItem i = new ListItem();
                i.Value = skin;
                i.Text = skin;
                cboSkin.Items.Add(i);
            }

            var fillEnums = Enum.GetValues(typeof(Telerik.Charting.Styles.FillType));
            foreach (var value in fillEnums)
            {
                ListItem i = new ListItem();
                i.Value = value;
                i.Text = value.ToString();
                //TODO: ComplexGradient, once activated, other types can be used at all. Refresh does not work on it.
                if (i.Text != "ComplexGradient") cboFillBy.Items.Add(i);
            }

            foreach (ChartSeries i in seriesCollection)
            {
                ListItem item = new ListItem();
                item.Text = i.Name;
                item.Value = i;
                lstSeries.Items.Add(item);
            }

            cboSkin.Text = m_Skin;
            cboFillBy.Text = m_FillType.ToString();
            pboPlotBackColor.BackColor = m_Color;
            if (lstSeries.Items.Count > 0) lstSeries.SelectedIndex = 0;
        }

        private void lstSeries_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem item = (ListItem)lstSeries.SelectedItem;
            ChartSeries series = (ChartSeries)item.Value;
            pboSeriesColor.BackColor = series.Appearance.FillStyle.MainColor;
            cboSeriesType.Text = series.Type.ToString();
            gboSeries.Text = String.Format("{0} Series", series.Name);
        }

        private void cboSeriesType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem item = (ListItem)lstSeries.SelectedItem;
            ChartSeries series = (ChartSeries)item.Value;
            ListItem i = (ListItem)cboSeriesType.SelectedItem;
            series.Type = (ChartSeriesType)i.Value;
        }

        private void cboSkin_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_Skin = cboSkin.Text;   
        }

        public string SelectedSkin
        {
            get
            {
                return m_Skin;
            }
            set
            {
                m_Skin = value;
                cboSkin.Text = m_Skin;
            }
        }

        public Color SelectedColor
        {
            get
            {
                return m_Color;
            }
            set
            {
                m_Color = value;
                pboPlotBackColor.BackColor = m_Color;
            }
        }

        public Telerik.Charting.Styles.FillType SelectedFillType
        {
            get
            {
                return m_FillType;
            }
            set
            {
                m_FillType = value;
                cboFillBy.Text = m_FillType.ToString();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void pboSeriesColor_Click(object sender, EventArgs e)
        {
            colorDialog.Color = pboSeriesColor.BackColor;
            DialogResult response = colorDialog.ShowDialog(this);
            if (response == System.Windows.Forms.DialogResult.OK)
            {
                pboSeriesColor.BackColor = colorDialog.Color;
                ListItem item = (ListItem)lstSeries.SelectedItem;
                ChartSeries series = (ChartSeries)item.Value;
                series.Appearance.FillStyle.MainColor = colorDialog.Color;
            }
        }

        private void pboPlotBackColor_Click(object sender, EventArgs e)
        {
            colorDialog.Color = pboPlotBackColor.BackColor;
            DialogResult response = colorDialog.ShowDialog(this);
            if (response == System.Windows.Forms.DialogResult.OK)
            {
                pboPlotBackColor.BackColor = colorDialog.Color;
                m_Color = colorDialog.Color;
            }
        }

        private void cboFillBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItem item = (ListItem)cboFillBy.SelectedItem;
            m_FillType = (Telerik.Charting.Styles.FillType)item.Value;
        }
    }
}
